using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Crab.Workflow;
using System.Workflow.Activities;
using System.Workflow;
using System.Workflow.ComponentModel;
using Crab.Runtime.Contract;
using Crab.Web.Security;
using Crab.Services.Proxy;

namespace Crab.WorkflowClient
{
    public partial class MainForm : Form
    {
        private WorkflowDesignerControl Designer;
        public MainForm()
        {
            InitializeComponent();

            InitWorkflowDesignerControl();

        }

        private void Login(bool cancelToExit)
        {
            LoginForm form = new LoginForm();

            if (form.ShowDialog() == DialogResult.OK)
            {
                GetCurrentWorkflow();
            }
            else
            {
                if (cancelToExit)
                {
                    Application.Exit();
                }
                else
                {
                    Logoff();
                }
            }
        }

        private void Logoff()
        {
            XomlFile = "";
            this.Designer.ShowDefaultWorkflow();
        }

	

        #region Properties

        private string xomlFile;

        public string XomlFile
        {
            get { return xomlFile; }
            set { xomlFile = value; }
        }
	
        private string supportAssemblyPath = Application.StartupPath+@"\Crab.Workflow.dll";
        #endregion

        /// <summary>
        /// Inits the workflow designer control.
        /// </summary>
        private void InitWorkflowDesignerControl()
        {
            Designer = new WorkflowDesignerControl(this);
            Designer.Dock = DockStyle.Fill;
            Designer.Name = "WorkflowDesignerControl";
            this.panel1.Controls.Add(Designer);
        }

        /// <summary>
        /// Gets the current workflow.
        /// </summary>
        private void GetCurrentWorkflow()
        {
            WorkflowDefinition worflowDefinition = WorkflowProxy.GetWorkflowDefinition(0);
            //string content = this.WorkflowService.GetXomlByTenantID(this.TenantId,this.UserId);

            this.XomlFile = TenantContext.TenantName + ".xoml";
            File.WriteAllText(XomlFile, worflowDefinition.Xoml);
            if (!string.IsNullOrEmpty(this.XomlFile))
            {
                this.Designer.LoadWorkflow(this.XomlFile, this.supportAssemblyPath);
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            ClearWorkflowFile();
        }

        /// <summary>
        /// Clears the workflow files.
        /// </summary>
        private void ClearWorkflowFile()
        {
            if (File.Exists(TenantContext.TenantName + ".xoml"))
                File.Delete(TenantContext.TenantName + ".xoml");

            //if (File.Exists(this.TenantId + ".rules"))
            //    File.Delete(this.TenantId + ".rules");

            if (File.Exists("Myfile.xoml"))
                File.Delete("Myfile.xoml");
        }

        private void loginToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Login(false);
            loginToolStripMenuItem.Enabled = false;
            logoutToolStripMenuItem.Enabled = true;
        }

        private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Logoff();
            loginToolStripMenuItem.Enabled = true;
            logoutToolStripMenuItem.Enabled = false;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void getCurrentWorkflowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.XomlFile))
                    return;

                GetCurrentWorkflow();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void MainForm_Load(object sender, EventArgs e)
        {

            Login(true);
        }

        private void mnuSave_Click(object sender, EventArgs e)
        {
            try
            {
                Designer.Save(TenantContext.TenantName + ".xoml");
                WorkflowProxy.UpdateWorkflowDefinition(0, File.ReadAllText(TenantContext.TenantName + ".xoml"), "");
                MessageBox.Show("Saved Successfully!");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }    
}